﻿package app.view 
{
    import app.view.event.UIEvent;
    import app.view.vc.NumberBtns;
    import flash.events.Event;
    import org.puremvc.as3.interfaces.IMediator;
    import org.puremvc.as3.interfaces.INotification;
    import org.puremvc.as3.patterns.mediator.Mediator;
    import app.model.*;
    import app.model.vo.Config;
    import app.ApplicationFacade;
    
    /**
    * Author lzyy
    * Email:healdream@gmail.com
    * Blog:http://lab.lzyy.name/blog
    */
    public class NumberBtnsMediator extends Mediator implements IMediator
    {
        public static const NAME:String = 'NumberBtnsMediator';
        private var _xmlLength:Number;
        public function NumberBtnsMediator(vc) 
        {
            super(NAME, vc);
            _addBtns();
            _arrangeBtns();
            btns.addEventListener(NumberBtns.BTN_CLICKED, _onBtnClicked);
            var dp:DataProxy = facade.retrieveProxy(DataProxy.NAME) as DataProxy;
            var data = dp.getData();
            _xmlLength = data.title.length;
        }
        
        private function _onBtnClicked(evt:UIEvent)
        {
            sendNotification(ApplicationFacade.NUMBTN_CLIKCED, { index:evt.data.index } );
        }
        
        override public function listNotificationInterests():Array 
        {
            return [ApplicationFacade.IMG_CHANGED];            
        }

        override public function handleNotification( note:INotification ):void
        {
            switch(note.getName())
            {
                case ApplicationFacade.IMG_CHANGED:
                    var data = note.getBody();
                    _highLightNum(data.index);
                    break;
            }
        }
        
        private function _highLightNum(n)
        {
            var dest = btns.getChildByName('btn'+n);
            //trace(dest);
            for (var i = 0; i < _xmlLength; i++)
            {
                var numBtn = btns.getChildByName('btn' + i);
                numBtn.gotoAndStop(1);
            }
            dest.gotoAndStop(2);
        }
        
        private function _addBtns()
        {
            var dp:DataProxy = facade.retrieveProxy(DataProxy.NAME) as DataProxy;
            var data = dp.getData();
            var xmlLength:Number = data.title.length;
            btns.addBtn(xmlLength);
        }
        
        private function _arrangeBtns()
        {
            var sp:StageProxy = facade.retrieveProxy(StageProxy.NAME) as StageProxy;
            var stageWidth = sp.containerWidth;
            var stageHeight = sp.containerHeight;
            switch(Config.ALIGN_TYPE)
            {
                case 'TL':
                    btns.x = btns.y = 0;
                    break;
                case 'TR':
                    btns.x = stageWidth - btns.width;
                    btns.y = 0;
                    break;
                case 'BL':
                    btns.x = 0;
                    btns.y = stageHeight - btns.height;
                    break;
                case 'BR':
                    btns.x = stageWidth - btns.width;
                    btns.y = stageHeight - btns.height;
            }
        }
        
        public function get btns()
        {
            return viewComponent as NumberBtns;
        }
        
    }
    
}